<?
function getFiles() {
	global $dbname, $db, $db_selected;
	$sql = "SELECT * from transCategories WHERE CatType = 'F' AND ParentID = '0' ORDER BY CatName";
	$result = mysql_db_query($dbname,$sql);
	$fileList = '';
	$files = 0;
	while ($value = mysql_fetch_assoc($result)) {
		$catID   = $value[CID];		$catName = stripslashes($value[CatName]);
		
		// See if we have any files at all under this category
		if ($_SESSION['userLevel'] == 1) {
			$sqlCount = "SELECT COUNT(SFID) AS NoFiles from transSharedFiles WHERE CatID = '$catID'";
		} else {
			$sqlCount = "SELECT COUNT(SFID) AS NoFiles from transSharedFiles WHERE CatID = '$catID' AND Approved = 1";
		}
		$resultCount = mysql_db_query($dbname,$sqlCount);
		$filesTotalArray = mysql_fetch_assoc($resultCount);
		$filesTotal = $filesTotalArray[NoFiles];
		
		if ($filesTotal == 1) {
			$fileText = 'file';
		} else {
			$fileText = 'files';
		}
		
		// Render header stuff
		$fileList .= '<div class="emphBox"><div class="emphBoxInner">';
		$fileList .= '<div onClick="filesViewObj.viewFilesByCat('.$catID.'); return false;" title="Click to view files" style="cursor: pointer; float: left;">';
		$fileList .= '<h1 class="arrowDown" id="mainCatHeading'.$catID.'" style="margin-bottom: 0px;">'.$catName.' [<span id="noOfFiles'.$catID.'">'.$filesTotal.'</span> '.$fileText.']</h1>';
		$fileList .= '</div><div style="float: right;">';
		$fileList .= '<img src="images/buttonUploadFile.gif" style="border: none; cursor: pointer;" alt="Add new file" title="Add new file" onClick="filesSupportObj.openAddEditForm('.$catID.'); return false;" id="buttonAddFile'.$catID.'" />';
		$fileList .= '</div><div style="clear: both;"></div></div></div>';
		
		$fileList .= '<div id="addEditForm'.$catID.'" style="display: none;">';
		$fileList .= '<div id="iframeDiv'.$catID.'">';
		$fileList .= '<iframe id="fileFrame'.$catID.'" frameborder="0" scrolling="no" style="width: 100%; height: 400px;" class="uploadIFrame" src="includes/uploadFile.php?cID='.$catID.'"></iframe>';
		$fileList .= '</div><div id="uploadStatus'.$catID.'"></div>';
		$fileList .= '</div>';
		$fileList .= '<div id="fileList'.$catID.'" style="display: none;"><div id="scriptaculousNeedsMe">';
		
		if ($filesTotal<1) {
			$fileList .= 'There are no files in this category yet.';
		} else {
			// Check for sub-categories
			$sqlSub = "SELECT * from transCategories WHERE CatType = 'F' AND ParentID = '$catID' ORDER BY CatName ASC";
			$resultSub = mysql_db_query($dbname,$sqlSub);
			
			if (mysql_num_rows($resultSub)>0) {
				while ($valueSub = mysql_fetch_assoc($resultSub)) {
					$subCatID   = $valueSub[CID];
					$subCatName = $valueSub[CatName];
					if ($_SESSION['userLevel'] == 1) {	
						$sql2 = "SELECT * from transSharedFiles 
									WHERE 
									CatID = '$catID' AND SubCatID = '$subCatID' 
									ORDER BY FileTitle DESC";
					} else {
						$sql2 = "SELECT * from transSharedFiles 
									WHERE 
									CatID = '$catID' AND 
									SubCatID = '$subCatID' AND
									Approved = 1
									ORDER BY FileTitle DESC"; 	 
					}
					$result2 = mysql_db_query($dbname,$sql2);
					$rows2 = mysql_num_rows($result2);
					$fileList .= '<h3 style="margin-top: 15px; margin-bottom: 0px;">'.$subCatName.'</h3>';
					$fileList .= '<div id="fileListSubCat'.$subCatID.'">';
					if ($rows2>0) {
						// Render table
						$fileList .= renderFileTable($result2);
					} else {
						$fileList .= '<span style="color: #999;">There are no files in this category yet.</span>';	
					}
					$fileList .= '</div>';
				}	
			} else {
				if ($_SESSION['userLevel'] == 1) {
					$sql2 = "SELECT * from transSharedFiles WHERE CatID = '$catID' ORDER BY FileTitle DESC"; 
				} else {
					$sql2 = "SELECT * from transSharedFiles 
								WHERE CatID = '$catID' AND
								Approved = 1
								ORDER BY FileTitle DESC"; 
				}
				$result2 = mysql_db_query($dbname,$sql2);
				$rows2 = mysql_num_rows($result2);
				
				if ($rows2>0) {
					// Render table
					$fileList .= renderFileTable($result2);
				} else {
					$fileList .= '<span style="color: #999;">There are no files in this category yet.</span>';	
				}
			}
		}
		
		$fileList .= '</div></div><div style="height: 20px;"></div>';				}			return $fileList;}

function refreshFiles($catID=null,$subCatID=null) {
	global $dbname, $db, $db_selected;
	
	$fileList = '';
	if (!empty($subCatID)) {
		if ($_SESSION['userLevel'] == 1) {	
			$sql2 = "SELECT * from transSharedFiles
						WHERE 
						CatID = '$catID' AND SubCatID = '$subCatID' 
						ORDER BY FileTitle DESC";
		} else {
			$sql2 = "SELECT * from transSharedFiles 
						WHERE 
						CatID = '$catID' AND 
						SubCatID = '$subCatID' AND
						Approved = 1
						ORDER BY FileTitle DESC"; 	 
		}		
	} else {
		if ($_SESSION['userLevel'] == 1) {
			$sql2 = "SELECT * from transSharedFiles WHERE CatID = '$catID' ORDER BY FileTitle DESC"; 
		} else {
			$sql2 = "SELECT * from transSharedFiles 
						WHERE CatID = '$catID' AND
						Approved = 1
						ORDER BY FileTitle DESC"; 
		}		
	}
	$result2 = mysql_db_query($dbname,$sql2);
	$rows2 = mysql_num_rows($result2);
	 
	if ($rows2>0) {
		// Render table
		$fileList .= renderFileTable($result2);
	} else {
		$fileList .= '<span style="color: #999;">There are no files in this category yet.</span>';	
	}		
	
	return $catID.'|'.$subCatID.'|'.$fileList;
}

function renderFileTable($resultSet) {
	$fileList = '<table class="dataTable" style="width: 100%;">';
	$fileList .= '<tr>';
		if ($_SESSION['userLevel'] == 1) {	
			$fileList .= '<th style="width: 5%;"></th>';
		}	
		$fileList .= '<th style="width: 65%;">Title</th>';
		$fileList .= '<th style="width: 10%;">Date</th>';
		$fileList .= '<th style="width: 10%;">Size</th>';
		
		if ($_SESSION['userLevel'] == 1) {		
			$fileList .= '<th style="width: 10%;">Approved</th>';
		}	
	$fileList .= '</tr>';
	while ($value2 = mysql_fetch_assoc($resultSet)) {
		if (($i % 2) > 0) { $class = 'altRow'; }
		else { $class = ''; }

		$fID      = $value2[SFID];
		$fTitle   = stripslashes($value2[FileTitle]);
		$fDesc    = stripslashes($value2[FileDesc]);
		$fName    = stripslashes($value2[FileName]);
		$fDate    = $value2[FileDate];
		$fSize    = convertFileSize($value2[FileSize]);
		$approved = $value2[Approved];
		
		if ($approved == 1) {
			$approveImage = 'iconCheckGreenOn.gif';
			$approveAlt   = 'This file is approved';
		} else {
			$approveImage = 'iconCheckGreenOff.gif';
			$approveAlt   = 'This file is not approved';
		}

		// Determine if filename is a web link
		if (substr($fName,0,4) == 'http') {
			$fURL = $fName;
		} else {
			$fURL = '../docs/District Documents/'.$fName;
		}

		$fileList .= '<tr class="'.$class.'" id="row'.$fID.'">';
		if ($_SESSION['userLevel'] == 1) {	
			$fileList .= '<td style="text-align: center;">';
			//$fileList .= '<a href="#" onClick="editFile('.$fID.'); return false;"><img src="images/buttonEditBig.gif" style="border: none;" alt="Edit" /></a>&nbsp;';
			$fileList .= '<a href="#" onClick="confirmDelete(\''.$fID.'\',\'file\'); return false;"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a></td>';
		}	
		$fileList .= '<td><a href="'.$fURL.'" target="_blank"><b>'.$fTitle.'</b></a></td>';
		$fileList .= '<td>'.mysql_to_date2($fDate).'</td>';
		$fileList .= '<td>'.$fSize.'</td>';
		if ($_SESSION['userLevel'] == 1) {	
			$fileList .= '<td id="approveContainer'.$fID.'" style="text-align: center;">';
			$fileList .= '<img src="images/'.$approveImage.'" alt="'.$approveAlt.'" title="'.$approveAlt.'" onClick="filesSupportObj.swapApproved('.$fID.'); return false;" style="cursor: pointer; border: none;" />';
			$fileList .= '</td>';
		}
		$fileList .= '</tr>';

		$i++;
		$files++;
	}
	$fileList .= '</table>';
	
	return $fileList;
}
function swapApproved($fID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT Approved from transSharedFiles WHERE SFID = '$fID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$approveState = $row[Approved];
	
	if ($approveState == 1) {
		$approveImage = 'iconCheckGreenOff.gif';
		$approveAlt   = 'This file is not approved';
		$newApproveState = 0;
	} else {
		$approveImage = 'iconCheckGreenOn.gif';
		$approveAlt   = 'This file is approved';
		$newApproveState = 1;
	}
	
	// Update approved
	$sql = "UPDATE transSharedFiles SET Approved = '$newApproveState'
			WHERE
			SFID = '$fID'";
	$result = mysql_db_query($dbname,$sql);
	$imageString = '<img src="images/'.$approveImage.'" alt="'.$approveAlt.'" title="'.$approveAlt.'" onClick="filesSupportObj.swapApproved('.$fID.'); return false;" style="cursor: pointer; border: none;" />';
	return $fID.'|'.$imageString;
}

function saveFile($phpString,$action=null) {	global $dbname, $db, $db_selected;
	// Parse phpString	$varArray = explode('|',$phpString);	$fileCat = $varArray[0];	$fTitle  = fieldToDB($varArray[1]);	$fDesc   = $varArray[2];	$fFile   = fieldToDB($varArray[3]);
	if ($action == 'add') {		$sql = "INSERT into transSharedFiles (				CatID,				FileTitle,				FileDesc,				FileName,				FileDate,				Active				) values (				'$fileCat',				'$fTitle',				'$fDesc',				'$fFile',				'".date('Y-m-d')."',				'1'				)";	} elseif ($action == 'edit') {
	}	$result = mysql_db_query($dbname,$sql);}
function getFileForEdit($fID) {
	// TODO: Create getFileForEdit function
}

function deleteFile($fID) {	global $dbname, $db, $db_selected, $rootDir;	// Get filename
	$sql = "SELECT CatID,FileName from transSharedFiles WHERE SFID = '$fID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$fileName = $row[FileName];
	$catID    = $row[CatID];
	
	$filePath = $rootDir.'docs/District Documents/'.$fileName;
	unlink($filePath);
	
	// Delete file information	$sql = "DELETE from transSharedFiles WHERE SFID = '$fID'";	$result = mysql_db_query($dbname,$sql);	
	return $fID.'|'.$catID;}
?>